﻿using HlELock.Core;
using HlELock.Core.Events;
using HlELock.Modules.Hotel.Agency;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace HlELock.Page.ViewModels
{
    public class SearchDeviceControlViewModel : BindableBase
    {
        public SearchDeviceControlViewModel()
        {

            Task.Run(SearchDevice);
            Descr = AppHelper.GetResourceString("ZhenZaiSouSuoSheBei");
        }

        private DelegateCommand _activeCommand;
        public DelegateCommand ActiveCommand =>
            _activeCommand ?? (_activeCommand = new DelegateCommand(ExecuteActiveCommand));

        /// <summary>
        /// 激活设备
        /// </summary>
        void ExecuteActiveCommand()
        {
            EventBus.StartDialogLoading(AppHelper.GetResourceString("ZhenZaiJiHuo"));
            if (!CheckValid(MyAppContext.Agency.ProvinceCode))
            {
                MessageBox.Show("经销商省份编码错误");
                return;
            }
            if (!CheckValid(MyAppContext.Agency.CityCode))
            {
                MessageBox.Show("经销商城市编码错误");
                return;
            }
            if (!CheckValid(MyAppContext.Agency.CountiesCode))
            {
                MessageBox.Show("经销商区/县编码错误");
                return;
            }
            var areaCode = $"01{MyAppContext.Agency.ProvinceCode.ToString().PadLeft(2, '0')}{MyAppContext.Agency.CityCode.ToString().PadLeft(2, '0')}{MyAppContext.Agency.CountiesCode.ToString().PadLeft(2, '0')}";
            MyAppContext.Device.ActiveDevice(areaCode, MyAppContext.Agency.AgencyCode);
            EventBus.StopDialogLoading();
            Descr = AppHelper.GetResourceString("JiHuoWanCheng");
            ShowActivated = Visibility.Hidden;

            bool CheckValid(string val)
            {
                var str = val.PadLeft(2, '0');
                return str != "00";
            }
        }

        private string _descr;
        public string Descr
        {
            get { return _descr; }
            set { SetProperty(ref _descr, value); }
        }
        private Visibility _showBusy;
        public Visibility ShowBusy
        {
            get { return _showBusy; }
            set { SetProperty(ref _showBusy, value); }
        }

        private Visibility _showActivated = Visibility.Collapsed;
        public Visibility ShowActivated
        {
            get { return _showActivated; }
            set { SetProperty(ref _showActivated, value); }
        }


        private void SearchDevice()
        {
            var lst = UsbHidHelper.GetDeviceList();
            var usb = lst.FirstOrDefault(f => f.VID == MyAppContext.VID && f.PID == MyAppContext.PID);

            if (usb == null)
            {
                Descr = AppHelper.GetResourceString("MeiYouZhaoDaoSheBei");
            }
            else
            {
                MyAppContext.Device = usb;
                Descr = AppHelper.GetResourceString("LianJieSheBei");
                if (usb.Open())
                {
                    Descr = AppHelper.GetResourceString("JianCeSheBei");
                    MyAppContext.Device.GetAgenyCode(agencyCode =>
                    {
                        if (agencyCode == "0000" || agencyCode == "FFFF" || agencyCode == "1234")
                        {
                            ShowActivated = Visibility.Visible;
                            Descr = AppHelper.GetResourceString("SheBeiHaiMeiYouJiHuo");
                        }
                        else if (agencyCode != MyAppContext.Agency.AgencyCode)
                        {
                            Descr = AppHelper.GetResourceString("DengLuZhangHaoYuSheBeiBuFu");
                        }
                        else
                        {
                            EventBus.CloseDialog();
                        }
                        ShowBusy = Visibility.Collapsed;
                    });

                }
                else
                {
                    Descr = AppHelper.GetResourceString("LianJieShiBai");
                }
            }
        }
    }
}
